package com.example.stock.aspect;

import com.example.stock.controller.dto.DailyDetailDTO;
import com.example.stock.controller.dto.SetOperation;
import com.example.stock.controller.vo.MonthDetailVO;
import com.example.stock.controller.vo.WeekDetailVO;
import com.example.stock.excel.StockOperate;
import com.example.stock.service.StockOperationService;
import com.example.stock.service.UserService;
import lombok.extern.log4j.Log4j2;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Log4j2
@Aspect
@Component
public class StockOperationAspect {

    @Autowired
    StockOperationService operationService;

    @Autowired
    UserService userService;

    private Boolean isLogin() {
        return userService.isLogin();
    }

    @Pointcut("execution(* com.example.stock.service.WeekDataService.getWeekData(..))")
    public void getWeekData() {}

    @AfterReturning(value = "getWeekData()", returning = "collect")
    public void buildWeekData1(JoinPoint joinPoint, List<WeekDetailVO> collect) {
        String stock = (String) joinPoint.getArgs()[0];
        buildWeekData(collect, stock);
    }

    private <T extends SetOperation> void buildWeekData(List<T> collect, String stock) {
        if (!isLogin()) return;

        // 获取操作记录
        Map<String, List<StockOperate>> weekFormatOperation = operationService.getWeekFormat(stock);
        if (weekFormatOperation == null) {
            return;
        }

        // 添加对应操作记录
        collect.forEach(item -> {
            synchronized (this) {
                String date = item.getDate();
                String searchKey = date.substring(0, 4) + ":" + date.substring(date.length() - 10);
                item.setOperation(weekFormatOperation.get(searchKey));
            }
        });
    }

    @Pointcut("execution(* com.example.stock.service.MonthDataService.getMonthData(..))")
    public void getMonthData() {}

    @AfterReturning(value = "getMonthData()", returning = "collect")
    public void buildMonthData(JoinPoint joinPoint, List<MonthDetailVO> collect) {

        if (!isLogin()) return;

        String stock = (String) joinPoint.getArgs()[0];

        // 获取操作记录
        Map<String, List<StockOperate>> monthFormatOperation = operationService.getMonthFormat(stock);
        if (monthFormatOperation == null) {
            return;
        }
        // 添加对应操作记录
        collect.forEach(monthDetailVO -> {
            monthDetailVO.setOperation(monthFormatOperation.get(monthDetailVO.getDate()));
        });
    }

    @Pointcut("execution(* com.example.stock.service.DailyDataService.getDailyData(..))")
    public void  getDailyDetail() {
    }

    @AfterReturning(value = "getDailyDetail()", returning = "collect")
    public void buildDailyDetail(JoinPoint joinPoint, List<DailyDetailDTO> collect) {
        String stock = (String) joinPoint.getArgs()[0];
        buildDailyData(collect, stock);
    }

    private  <T extends SetOperation> void buildDailyData(List<T>  collect, String stock) {
        if (!isLogin()) return;

        Map<String, List<StockOperate>> stockOperates = operationService.getDayFormat(stock);
        if (stockOperates == null) {
            return;
        }

        collect.forEach(item -> {
            synchronized (this) {
                item.setOperation(stockOperates.get(item.getDate()));
            }
        });
    }
}